package com.project.collectAPI.entity.saleByTagIdAPI;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.project.collectAPI.comm.SpiderTools;
import com.project.collectAPI.entity.MeiTuan.MeituanGoods;
import com.project.collectAPI.entity.MeiTuan.SystemParam;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/*根据tag_id获取销售属性*/
@Data
public class saleByTagIdAPI {

    private String reqAPI="https://waimaiopen.meituan.com/api/v1/gw/category/attr/list";

    //统一的时间戳
    private Long timestamp;

    //类目tag_id
    private long tag_id;

    //签名
    private String signValue;


    /*获取美团类目接口签名*/
    public String getSign() throws Exception {
        SystemParam systemParam=new SystemParam();
        Map<String,Object> map=new HashMap<>();
        map.put("timestamp",timestamp);  //timestamp尽量统一用一次
        map.put("app_id",systemParam.getAppId());
        map.put("tag_id",tag_id);
        return systemParam.getSign(map,reqAPI); //获取签名
    }

    public String getClassFromTagId(){
        SystemParam systemParam=new SystemParam();
        String reqUrl=reqAPI+"?app_id="+systemParam.getAppId()+"&timestamp="+timestamp+"&sig="+signValue+"&tag_id="+tag_id;
        //System.out.println("地址:"+reqUrl);
        String res= SpiderTools.HttpGet(reqUrl);
        return res;
    }


    //将美团后台返回的类目信息转化为销售默认属性
    /*
    * {
	"data": {
		"general_attrs": [{
			"attr_id": "1200000088",
			"attr_name": "品牌",
			"attr_value_type": "1",
			"need": "1",
			"character_type": "1,2,3,4",
			"text_max_length": "30",
			"sequence": 1,
			"support_extend": "0",
			"value_list": []
		}, {
			"attr_id": "1200000260",
			"attr_name": "功能",
			"attr_value_type": "2",
			"need": "1",
			"character_type": "1,2,3,4",
			"text_max_length": "30",
			"sequence": 3,
			"support_extend": "0",
			"value_list": [{
				"value_id": "1300010207",
				"value": "防偷窥"
			}, {
				"value_id": "1300007186",
				"value": "夜光"
			}, {
				"value_id": "1300005371",
				"value": "抑菌"
			}, {
				"value_id": "1300005387",
				"value": "超薄"
			}, {
				"value_id": "1300008869",
				"value": "防静电"
			}, {
				"value_id": "1300020098",
				"value": "防磨"
			}, {
				"value_id": "1300002623",
				"value": "防水"
			}, {
				"value_id": "1300010203",
				"value": "抗蓝光"
			}, {
				"value_id": "1300010204",
				"value": "防指纹"
			}, {
				"value_id": "1300009061",
				"value": "高清"
			}, {
				"value_id": "1300016304",
				"value": "其他"
			}, {
				"value_id": "1300010205",
				"value": "护眼膜"
			}, {
				"value_id": "1300020097",
				"value": "防爆"
			}, {
				"value_id": "1300000945",
				"value": "防尘"
			}, {
				"value_id": "1300007401",
				"value": "防摔"
			}]
		}, {
			"attr_id": "1200000145",
			"attr_name": "材质",
			"attr_value_type": "1",
			"need": "2",
			"character_type": "1,2,3,4",
			"text_max_length": "30",
			"sequence": 3,
			"support_extend": "0",
			"value_list": [{
				"value_id": "1300010210",
				"value": "液态纳米"
			}, {
				"value_id": "1300000249",
				"value": "其他"
			}, {
				"value_id": "1300010209",
				"value": "高清软膜"
			}, {
				"value_id": "1300002302",
				"value": "钢化膜"
			}, {
				"value_id": "1300002304",
				"value": "水凝膜"
			}]
		}, {
			"attr_id": "1200000094",
			"attr_name": "产地",
			"attr_value_type": "1",
			"need": "2",
			"character_type": "1,2,3,4",
			"text_max_length": "30",
			"sequence": 4,
			"support_extend": "0",
			"value_list": []
		}, {
			"attr_id": "1200000210",
			"attr_name": "商品类别",
			"attr_value_type": "1",
			"need": "1",
			"character_type": "1,2,3,4",
			"text_max_length": "30",
			"sequence": 5,
			"support_extend": "0",
			"value_list": [{
				"value_id": "1300014826",
				"value": "贴膜工具"
			}, {
				"value_id": "1300014827",
				"value": "后膜"
			}, {
				"value_id": "1300014830",
				"value": "曲面膜"
			}, {
				"value_id": "1300014831",
				"value": "前后膜"
			}, {
				"value_id": "1300014828",
				"value": "非全屏膜"
			}, {
				"value_id": "1300014829",
				"value": "镜头膜"
			}, {
				"value_id": "1300014832",
				"value": "前膜"
			}, {
				"value_id": "1300014833",
				"value": "全屏膜"
			}]
		}]
	    }
    }
    * */
    public static List<MeituanGoods.fooddata.skusObjects.SaleObject> convertSaleObj(String getClassFromTagIdResponseString,String BrandName){
        List<MeituanGoods.fooddata.skusObjects.SaleObject> SaleObjectList=null;
        if(!StringUtils.isEmpty(getClassFromTagIdResponseString)){
            JSONObject json=JSONObject.parseObject(getClassFromTagIdResponseString);
            String dataArray=json.getJSONObject("data").getString("general_attrs");
            //遍历销售属性
            JSONArray AttsArr=JSONArray.parseArray(dataArray);
            if(AttsArr.size()>0){
                SaleObjectList=new ArrayList<>(); //初始化对象

            }
            for (int i = 0; i < AttsArr.size(); i++) {
                JSONObject item=JSONObject.parseObject(AttsArr.get(i).toString());
                //1代表销售必填项
                if(item.getString("need").equals("1")){
                    MeituanGoods.fooddata.skusObjects.SaleObject saleObject=new MeituanGoods.fooddata.skusObjects.SaleObject();
                    //销售属性名称
                    String attrName=item.getString("attr_name");
                    saleObject.setAttrName(attrName);
                    saleObject.setAttrId(Long.valueOf(item.getString("attr_id")));
                    String valueList=item.getString("value_list");
                    JSONArray valueListArr=JSONArray.parseArray(valueList);
                    //填充默认属性
                    if(valueListArr.size()>0){
                        Long value_id=Long.valueOf(JSONObject.parseObject(valueListArr.get(0).toString()).getString("value_id"));
                        //设置value属性，支持自定义
                        String valueStr=JSONObject.parseObject(valueListArr.get(0).toString()).getString("value");


                        List<Object> vListArr=new ArrayList<>();
                        HashMap<String,Object> temp=new HashMap<>();
                        temp.put("valueId",value_id);
                        //设置ValueId属性
                        temp.put("value",valueStr);
                        vListArr.add(temp);
                        saleObject.setValueList(vListArr);
                       /* saleObject.setValueId(value_id);
                        //设置value属性，支持自定义
                        String valueStr=JSONObject.parseObject(valueListArr.get(0).toString()).getString("value");
                        saleObject.setValue(valueStr);*/
                    }
                    else{
                        //自定义销售属性
                        //"attr_name": "品牌"
                        if(attrName.equals("品牌")){
                            //设置ValueId属性
                            saleObject.setAttrId(Long.valueOf(item.getString("attr_id")));
                            //ValueId 销售属性值id。当属性值录入方式为文本时，该参数无需上传；
                            // 若该销售属性可扩展则支持自定义： 1.负值 2.同一个商品的同一个销售属性内唯一
                            /*saleObject.setValueId(1904257L);
                            saleObject.setValue(BrandName); //设置品牌*/
                            List<Object> vListArr=new ArrayList<>();
                            HashMap<String,Object> temp=new HashMap<>();
                            temp.put("valueId",1904257L);
                            //设置ValueId属性
                            temp.put("value",BrandName);
                            vListArr.add(temp);
                            saleObject.setValueList(vListArr);
                        }
                    }
                    SaleObjectList.add(saleObject);
                }
            } //for End
        }
        return SaleObjectList;
    }












    //普通属性
    public static List<MeituanGoods.fooddata.commonAttrClass> convertCommonAttrObj(String getClassFromTagIdResponseString,String BrandName){
        List<MeituanGoods.fooddata.commonAttrClass> CommonObjectList=null;
        if(!StringUtils.isEmpty(getClassFromTagIdResponseString)){
            JSONObject json=JSONObject.parseObject(getClassFromTagIdResponseString);
            String dataArray=json.getJSONObject("data").getString("general_attrs");
            //遍历销售属性
            JSONArray AttsArr=JSONArray.parseArray(dataArray);
            if(AttsArr.size()>0){
                CommonObjectList=new ArrayList<>(); //初始化对象
            }

            //品牌 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200000088L,"品牌",1904257L,BrandName));
            //国产/进口 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200000132L,"国产/进口",1300000003L,"国产"));
            //品种 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200000202L,"品种",1300015567L,"普通玉米"));
            //储存方式 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200000352L,"储存方式",114082L,"常温"));
            //果肉颜色 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200004649L,"果肉颜色",1300016304L,"其他"));
            //拼数 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200004449L,"果肉颜色",1300015654L,"单拼"));
            //商品类别 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200000210L,"商品类别",1300025225L,"其他单拼果切/果拼"));
            //品规 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200000289L,"品规",1300004255L,"未区分品规"));
            //品规 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200000275L,"是否有机",1300005199L,"非有机"));
            //口味 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200000133L,"口味",1300000249L,"其他"));
            //是否含糖 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200000204L,"是否含糖",1300000999L,"低糖"));
            //等级 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200000156L,"等级",1300000249L,"其他"));
            //类别 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200000245L,"类别",1300027692L,"混装"));
            //工艺 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200000335L,"工艺",1300027692L,"生熟组合"));
            //适用年龄 -- 自定添加需求属性
            CommonObjectList.add(AddAttrClass(1200000389L,"适用年龄",1300002258L,"通用"));

            for (int i = 0; i < AttsArr.size(); i++) {
                JSONObject item=JSONObject.parseObject(AttsArr.get(i).toString());
                //1代表销售必填项
                if(item.getString("need").equals("1")){
                    MeituanGoods.fooddata.commonAttrClass CommonObject=new MeituanGoods.fooddata.commonAttrClass();
                    //销售属性名称
                    String attrName=item.getString("attr_name");
                    System.out.println("销售属性名称11...............:"+attrName);
                    CommonObject.setAttrName(attrName);
                    CommonObject.setAttrId(Long.valueOf(item.getString("attr_id")));
                    String valueList=item.getString("value_list");
                    JSONArray valueListArr=JSONArray.parseArray(valueList);
                    //填充默认属性
                    if(valueListArr.size()>0){
                        Long value_id=Long.valueOf(JSONObject.parseObject(valueListArr.get(0).toString()).getString("value_id"));
                        //设置value属性，支持自定义
                        String valueStr=JSONObject.parseObject(valueListArr.get(0).toString()).getString("value");

                        List<MeituanGoods.fooddata.commonAttrClass.valueObj> vListArr=new ArrayList<>();
                        /*HashMap<String,Object> temp=new HashMap<>();
                        temp.put("valueId",value_id);
                        //设置ValueId属性
                        temp.put("value",valueStr);*/
                        MeituanGoods.fooddata.commonAttrClass.valueObj vobj=new MeituanGoods.fooddata.commonAttrClass.valueObj();
                        vobj.valueId=value_id;
                        vobj.value=valueStr;
                        vListArr.add(vobj);
                        CommonObject.setValueList(vListArr);
                    }
                    else{
                        //自定义销售属性
                        //"attr_name": "品牌"
                        if(attrName.equals("品牌")){
                            //设置ValueId属性
                            CommonObject.setAttrId(Long.valueOf(item.getString("attr_id")));
                            CommonObject.setAttrName(attrName);
                            //ValueId 销售属性值id。当属性值录入方式为文本时，该参数无需上传；
                            // 若该销售属性可扩展则支持自定义： 1.负值 2.同一个商品的同一个销售属性内唯一
                            List<MeituanGoods.fooddata.commonAttrClass.valueObj> vListArr=new ArrayList<>();
                            MeituanGoods.fooddata.commonAttrClass.valueObj vobj=new MeituanGoods.fooddata.commonAttrClass.valueObj();
                            vobj.valueId=1904257L;
                            vobj.value=BrandName;
                            vListArr.add(vobj);
                            CommonObject.setValueList(vListArr);
                        }
                        if(attrName.equals("像素")){
                            //设置ValueId属性
                            CommonObject.setAttrId(Long.valueOf(item.getString("attr_id")));
                            CommonObject.setAttrName(attrName);
                            //ValueId 销售属性值id。当属性值录入方式为文本时，该参数无需上传；
                            // 若该销售属性可扩展则支持自定义： 1.负值 2.同一个商品的同一个销售属性内唯一
                            List<MeituanGoods.fooddata.commonAttrClass.valueObj> vListArr=new ArrayList<>();
                            MeituanGoods.fooddata.commonAttrClass.valueObj vobj=new MeituanGoods.fooddata.commonAttrClass.valueObj();
                            vobj.valueId=-1L;
                            vobj.value="2000";
                            vListArr.add(vobj);
                            CommonObject.setValueList(vListArr);
                        }


                    }
                    CommonObjectList.add(CommonObject);
                }
            } //for End
        }
        return CommonObjectList;
    }


    public static MeituanGoods.fooddata.commonAttrClass AddAttrClass(Long attrId,String attrName,Long valueId,String valueString){
        MeituanGoods.fooddata.commonAttrClass CommonObject=new MeituanGoods.fooddata.commonAttrClass();
        CommonObject.setAttrId(attrId); //1200000132L
        CommonObject.setAttrName(attrName); //"国产/进口"
        List<MeituanGoods.fooddata.commonAttrClass.valueObj> gcListArr=new ArrayList<>();
        MeituanGoods.fooddata.commonAttrClass.valueObj gcobj=new MeituanGoods.fooddata.commonAttrClass.valueObj();
        gcobj.valueId=valueId; //1300000003L
        gcobj.value=valueString; //国产
        gcListArr.add(gcobj);
        CommonObject.setValueList(gcListArr);
        return CommonObject;
    }

} /* class End */
